package com.example.demo.controller;


import com.example.demo.model.Contact;
import com.example.demo.model.Member;
import com.example.demo.service.MemberService;
import com.example.demo.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {
    // 注入 MemberService 依赖
    @Autowired
    private MemberService MemberService;


    @RequestMapping("/add")
    public ResponseBodyMessage<Boolean> add (@RequestParam("name") String name,
                                             @RequestParam("introduction") String introduction,
                                             @RequestParam("position") String position,
                                             @RequestParam("sex") String sex
    ) {
        // 非空判断
        if (StringUtils.hasLength(name) ||
                StringUtils.hasLength(introduction) ||
                StringUtils.hasLength(position) ||
                StringUtils.hasLength(sex)
        ) {
            // 调用 MemberService.addMember() 处理请求
            return MemberService.add(name,introduction,position,sex);
        } else {
            // 参数错误
            return new ResponseBodyMessage<>(400, "请求参数错误",false);
        }
    }

    @RequestMapping("/delete")
    public ResponseBodyMessage<Boolean> delete (@RequestParam("id") Integer id){
        if(id!=null){
            return MemberService.delete(id);
        }
        else{
            return new ResponseBodyMessage<>(400, "请求参数错误",false);
        }
    }

    @RequestMapping("/getAll")
    public ResponseBodyMessage<List<Member>> getAll () {
        return MemberService.getAll();
    }

    @RequestMapping("/get")
    public ResponseBodyMessage<Member> get (@RequestParam ("id")Integer id) {
        // 非空判断
        if (id != null ) {
            return MemberService.get(id);
        } else {
            // 参数错误
            return new ResponseBodyMessage<>(400, "请求参数错误",null);
        }
    }
}
